<?php

namespace app\admin\controller\merchant;

/**
 * 店铺管理
 *
 */
class Store extends Base
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = loader('admin')->loadModel('tysj\\Store');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->param('filter');
            $filter = json_decode($filter, true);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $search = $this->request->param('search');
            $model = $this->model->with(['user']);

            if($search) {
                $where = [
                    'id|uid|name|address|description|sub_mchid' => ['like', "%" . $search . '%']
                ];
                $model->whereOr($where);
                $model->whereOr(function($query) use ($search) {
                    $userModel = loadModel('tyyh\\User');
                    $uids = $userModel->where('phone', 'like', '%' . $search . '%')->column('id');
                    $query->whereIn('uid', $uids);
                });
            } else {
                $model->where($where);
                if(isset($filter['user_phone|user_phone'])) {
                    $model->where(function($query) use ($filter) {
                        $userModel = loadModel('tyyh\\User');
                        $uids = $userModel->where('phone', 'like', '%' . $filter['user_phone|user_phone'] . '%')->column('id');
                        $query->whereIn('uid', $uids);
                    });
                }
            }

            $model->order($sort, $order);
            $list = $model->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $loader = loader();
        $typeOptions = $loader->loadEnum('StoreType')->get_options();
        $invoiceOptions = $loader->loadEnum('Invoice')->get_show_options();
        $statusOptions = $loader->loadEnum('StoreStatus')->get_show_options();
        $examineOptions = $loader->loadEnum('StoreExamine')->get_show_options();

        $this->assign('typeOptions', $typeOptions);
        $this->assign('invoiceOptions', $invoiceOptions);
        $this->assign('statusOptions', $statusOptions);
        $this->assign('examineOptions', $examineOptions);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $row = $this->request->post('row/a');
            $this->model->save($row, ['id' => $ids]);
            $this->success();
        }
        $row = $this->model->where(['id' => $ids])->find();
        $options = loadEnum('StoreType')->get_options();
        $this->assign('options', $options);
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function examine($ids = null)
    {
        $examineEnum = loadEnum('StoreExamine');
        if ($this->request->isAjax()) {
            $row = $this->request->post('row/a');
            $validate = loader('admin')->loadValidate('Store');
            if(!$validate->scene('save')->check($row)){
                $this->error($validate->getError());
            }
            $statusEnum = loadEnum('StoreStatus');

            if($row['examine'] != $examineEnum->facescan()) {
                $row['back_url'] = '';
            }
            if($row['examine'] != $examineEnum->complete()) {
                $row['sub_mchid'] = '';
            }
            if($row['examine'] != $examineEnum->code2key('jujueshenhe')) {
                $row['remark'] = '';
            }
            $orderModel = loadOnly('Model')->getModel('jy\\Order');
            $model =  loadOnly('Model')->getModel('tysj\Store');
            $process = loadProcess('StoreInfo');
            try {
                $orderModel->startTrans();
                $model->startTrans();
                $model->allowField(['examine', 'back_url', 'sub_mchid', 'remark'])->save($row, ['id' => $row['id']]);
                if($row['examine'] != $examineEnum->complete() && $row['status'] != $statusEnum->hideStatus()) {
                    $process->hide_ipl($row['id']);
                }
                $orderModel->commit();
                $model->commit();
            } catch (\Exception $e) {
                $orderModel->rollback();
                $model->rollback();
                $this->error($e->getMessage());
            }
            
            return $this->success();
        }
        $row = $this->model->find($ids);
        $options = $examineEnum->get_options();
        $facescan = $examineEnum->facescan();
        $complete = $examineEnum->complete();
        $refuse = $examineEnum->code2key('jujueshenhe');
        $this->assign('options', $options);
        $this->assign('facescan', $facescan);
        $this->assign('complete', $complete);
        $this->assign('refuse', $refuse);
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    //营业执照
    public function business_license() {
        $id = $this->request->get('id');
        $model = loader('admin')->loadModel('tysj\\BusinessLicense');
        $row = $model->where(['sid' => $id])->find();
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    //法人信息
    public function corporation() {
        $id = $this->request->get('id');
        $model = loader('admin')->loadModel('tysj\\Corporation');
        $row = $model->where(['sid' => $id])->find();
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    //店铺银行卡信息
    public function bank() {
        $id = $this->request->get('id');
        $model = loader('admin')->loadModel('tysj\\StoreBank');
        $row = $model->where(['sid' => $id])->find();
        $this->assign('row', $row);
        return $this->view->fetch();
    }

    //店铺详情
    public function info($id = null) {
        $row = $this->model->find($id);
        $this->assign('row', $row);
        return $this->view->fetch();
    }
}
